﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.Graphics.Effects;

namespace Terraria.ModLoader
{
	// Token: 0x02000175 RID: 373
	public static class SurfaceBgStyleLoader
	{
		// Token: 0x06000F21 RID: 3873 RVA: 0x0040141C File Offset: 0x003FF61C
		internal static int ReserveBackgroundSlot()
		{
			int result = SurfaceBgStyleLoader.nextSurfaceBgStyle;
			SurfaceBgStyleLoader.nextSurfaceBgStyle++;
			return result;
		}

		// Token: 0x06000F22 RID: 3874 RVA: 0x0000E54B File Offset: 0x0000C74B
		public static ModSurfaceBgStyle GetSurfaceBgStyle(int style)
		{
			if (style < 10 || style >= SurfaceBgStyleLoader.nextSurfaceBgStyle)
			{
				return null;
			}
			return SurfaceBgStyleLoader.surfaceBgStyles[style - 10];
		}

		// Token: 0x06000F23 RID: 3875 RVA: 0x0000E56A File Offset: 0x0000C76A
		internal static void ResizeAndFillArrays()
		{
			Array.Resize<float>(ref Main.bgAlpha, SurfaceBgStyleLoader.nextSurfaceBgStyle);
			Array.Resize<float>(ref Main.bgAlpha2, SurfaceBgStyleLoader.nextSurfaceBgStyle);
		}

		// Token: 0x06000F24 RID: 3876 RVA: 0x0000E58A File Offset: 0x0000C78A
		internal static void Unload()
		{
			SurfaceBgStyleLoader.nextSurfaceBgStyle = 10;
			SurfaceBgStyleLoader.surfaceBgStyles.Clear();
		}

		// Token: 0x06000F25 RID: 3877 RVA: 0x0040143C File Offset: 0x003FF63C
		public static void ChooseStyle(ref int style)
		{
			if (!GlobalBgStyleLoader.loaded)
			{
				return;
			}
			foreach (ModSurfaceBgStyle modSurfaceBgStyle in SurfaceBgStyleLoader.surfaceBgStyles)
			{
				if (modSurfaceBgStyle.ChooseBgStyle())
				{
					style = modSurfaceBgStyle.Slot;
				}
			}
			foreach (GlobalBgStyleLoader.DelegateChooseSurfaceBgStyle delegateChooseSurfaceBgStyle in GlobalBgStyleLoader.HookChooseSurfaceBgStyle)
			{
				delegateChooseSurfaceBgStyle(ref style);
			}
		}

		// Token: 0x06000F26 RID: 3878 RVA: 0x004014BC File Offset: 0x003FF6BC
		public static void ModifyFarFades(int style, float[] fades, float transitionSpeed)
		{
			if (!GlobalBgStyleLoader.loaded)
			{
				return;
			}
			ModSurfaceBgStyle surfaceBgStyle = SurfaceBgStyleLoader.GetSurfaceBgStyle(style);
			if (surfaceBgStyle != null)
			{
				surfaceBgStyle.ModifyFarFades(fades, transitionSpeed);
			}
			foreach (Action<int, float[], float> action in GlobalBgStyleLoader.HookModifyFarSurfaceFades)
			{
				action(style, fades, transitionSpeed);
			}
		}

		// Token: 0x06000F27 RID: 3879 RVA: 0x00401504 File Offset: 0x003FF704
		public static void DrawFarTexture()
		{
			if (!GlobalBgStyleLoader.loaded)
			{
				return;
			}
			foreach (ModSurfaceBgStyle modSurfaceBgStyle in SurfaceBgStyleLoader.surfaceBgStyles)
			{
				int slot = modSurfaceBgStyle.Slot;
				Main.backColor = Main.trueBackColor;
				Main.backColor.R = (byte)((float)Main.backColor.R * Main.bgAlpha2[slot]);
				Main.backColor.G = (byte)((float)Main.backColor.G * Main.bgAlpha2[slot]);
				Main.backColor.B = (byte)((float)Main.backColor.B * Main.bgAlpha2[slot]);
				Main.backColor.A = (byte)((float)Main.backColor.A * Main.bgAlpha2[slot]);
				if (Main.bgAlpha2[slot] > 0f)
				{
					int num = modSurfaceBgStyle.ChooseFarTexture();
					if (num >= 0 && num < Main.backgroundTexture.Length)
					{
						Main.instance.LoadBackground(num);
						for (int i = 0; i < Main.instance.bgLoops; i++)
						{
							Main.spriteBatch.Draw(Main.backgroundTexture[num], new Vector2((float)(Main.instance.bgStart + Main.bgW * i), (float)Main.instance.bgTop), new Rectangle?(new Rectangle(0, 0, Main.backgroundWidth[num], Main.backgroundHeight[num])), Main.backColor, 0f, default(Vector2), Main.bgScale, SpriteEffects.None, 0f);
						}
					}
				}
			}
		}

		// Token: 0x06000F28 RID: 3880 RVA: 0x004016B0 File Offset: 0x003FF8B0
		public static void DrawMiddleTexture()
		{
			if (!GlobalBgStyleLoader.loaded)
			{
				return;
			}
			foreach (ModSurfaceBgStyle modSurfaceBgStyle in SurfaceBgStyleLoader.surfaceBgStyles)
			{
				int slot = modSurfaceBgStyle.Slot;
				Main.backColor = Main.trueBackColor;
				Main.backColor.R = (byte)((float)Main.backColor.R * Main.bgAlpha2[slot]);
				Main.backColor.G = (byte)((float)Main.backColor.G * Main.bgAlpha2[slot]);
				Main.backColor.B = (byte)((float)Main.backColor.B * Main.bgAlpha2[slot]);
				Main.backColor.A = (byte)((float)Main.backColor.A * Main.bgAlpha2[slot]);
				if (Main.bgAlpha2[slot] > 0f)
				{
					int num = modSurfaceBgStyle.ChooseMiddleTexture();
					if (num >= 0 && num < Main.backgroundTexture.Length)
					{
						Main.instance.LoadBackground(num);
						for (int i = 0; i < Main.instance.bgLoops; i++)
						{
							Main.spriteBatch.Draw(Main.backgroundTexture[num], new Vector2((float)(Main.instance.bgStart + Main.bgW * i), (float)Main.instance.bgTop), new Rectangle?(new Rectangle(0, 0, Main.backgroundWidth[num], Main.backgroundHeight[num])), Main.backColor, 0f, default(Vector2), Main.bgScale, SpriteEffects.None, 0f);
						}
					}
				}
			}
		}

		// Token: 0x06000F29 RID: 3881 RVA: 0x0040185C File Offset: 0x003FFA5C
		public static void DrawCloseBackground(int style)
		{
			if (!GlobalBgStyleLoader.loaded)
			{
				return;
			}
			if (Main.bgAlpha[style] <= 0f)
			{
				return;
			}
			ModSurfaceBgStyle surfaceBgStyle = SurfaceBgStyleLoader.GetSurfaceBgStyle(style);
			if (surfaceBgStyle != null && surfaceBgStyle.PreDrawCloseBackground(Main.spriteBatch))
			{
				Main.bgScale = 1.25f;
				Main.instance.bgParallax = 0.37;
				float num = 1800f;
				float num2 = 1750f;
				int num3 = surfaceBgStyle.ChooseCloseTexture(ref Main.bgScale, ref Main.instance.bgParallax, ref num, ref num2);
				if (num3 >= 0 && num3 < Main.backgroundTexture.Length)
				{
					Main.instance.LoadBackground(num3);
					Main.bgScale *= 2f;
					Main.bgW = (int)((float)Main.backgroundWidth[num3] * Main.bgScale);
					SkyManager.Instance.DrawToDepth(Main.spriteBatch, 1f / (float)Main.instance.bgParallax);
					Main.instance.bgStart = (int)(-Math.IEEERemainder((double)Main.screenPosition.X * Main.instance.bgParallax, (double)Main.bgW) - (double)(Main.bgW / 2));
					Main.instance.bgTop = (int)((double)(-(double)Main.screenPosition.Y + Main.instance.screenOff / 2f) / (Main.worldSurface * 16.0) * (double)num + (double)num2) + (int)Main.instance.scAdj;
					if (Main.gameMenu)
					{
						Main.instance.bgTop = 320;
					}
					Main.instance.bgLoops = Main.screenWidth / Main.bgW + 2;
					if ((double)Main.screenPosition.Y < Main.worldSurface * 16.0 + 16.0)
					{
						for (int i = 0; i < Main.instance.bgLoops; i++)
						{
							Main.spriteBatch.Draw(Main.backgroundTexture[num3], new Vector2((float)(Main.instance.bgStart + Main.bgW * i), (float)Main.instance.bgTop), new Rectangle?(new Rectangle(0, 0, Main.backgroundWidth[num3], Main.backgroundHeight[num3])), Main.backColor, 0f, default(Vector2), Main.bgScale, SpriteEffects.None, 0f);
						}
					}
				}
			}
		}

		// Token: 0x040012A8 RID: 4776
		public const int vanillaSurfaceBgStyleCount = 10;

		// Token: 0x040012A9 RID: 4777
		private static int nextSurfaceBgStyle = 10;

		// Token: 0x040012AA RID: 4778
		internal static readonly IList<ModSurfaceBgStyle> surfaceBgStyles = new List<ModSurfaceBgStyle>();
	}
}
